# 构建队列类
from redis import StrictRedis
from pickle import dumps, loads
from MovieRequest import MovieRequest


class Queto():
    # 构建队列类
    def __init__(self):
        '''
        init redis connection
        '''
        self.db = StrictRedis(host='localhost', port=6379)

    def add(self, request):
        '''
        add request to queue
        :param request:request
        :return:result
        '''
        if isinstance(request, MovieRequest):
            return self.db.rpush('antispider', dumps(request))
        return False

    def pop(self):
        '''
        get next request
        return:NONE or Request
        '''
        if self.db.llen('antispider'):
            return loads(self.db.lpop('antispider'))
        return False

    def empty(self):
        return self.db.llen('antispider') == 0

    def clear(self):
        return self.db.delete('antispider')
